package com.songshuai.webapi.permission.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import com.songshuai.webapi.permission.dto.request.FunctionAddReqDto;
import com.songshuai.webapi.permission.dto.request.FunctionListReqDto;
import com.songshuai.webapi.permission.dto.request.FunctionUpdateReqDto;
import com.songshuai.webapi.permission.dto.response.FunctionListResDto;
import com.songshuai.webapi.permission.dto.response.FunctionsLoginResDto;
import com.songshuai.webapi.permission.model.YxFunctions;
import com.songshuai.webapi.permission.service.FunctionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api("功能管理")
@Slf4j
@RestController
@RequestMapping("/api/functions/")
public class FunctionsController extends BaseController{
    @Autowired
    private FunctionsService functionsService;

    @ApiOperation(value = "查询登录用户的功能列表")
    @GetMapping("queryLoginFunctionList")
    public QueryResponse<List<FunctionsLoginResDto>> queryLoginFunctionList(@RequestParam("appId")Integer appId){
        List<FunctionsLoginResDto> functionList = functionsService.queryLoginFunctionList(appId);
        return QueryResponse.setResData(functionList,ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "根据appId和功能名查询功能列表")
    @PostMapping("queryFunctionListByPage")
    public QueryResponse<List<FunctionListResDto>> queryFunctionListByPage(@Valid @RequestBody BaseParmDto<FunctionListReqDto> parmDto, BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        Page<Object> page = PageHelper.startPage(parmDto.getPageIndex(), parmDto.getPageSize());
        List<FunctionListResDto> functionList = functionsService.queryFunctionListByPage(parmDto.getParam());
        return QueryPageRes.setQueryResponseData(functionList, page.getTotal());
    }

    @ApiOperation(value = "新增功能")
    @PostMapping("addFunction")
    public QueryResponse addFunction(@Valid @RequestBody FunctionAddReqDto parmDto,BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        boolean lock = getTokenLock("/api/functions/addFunction");
        try {
            if(lock) {
                //功能key唯一
                YxFunctions yxFunctions = functionsService.queryFunctionByKey(parmDto.getFunctionKey());
                if (yxFunctions != null) {
                    return new QueryResponse(ResultStatus.ERROR_IS_EXIST);
                }
                boolean isSucc = functionsService.addFunction(parmDto);
                if (isSucc) {
                    return new QueryResponse();
                }
            }
        } catch (Exception e) {
            log.error("addFunction",e);
        } finally {
            relesesTokenLock("/api/functions/addFunction");
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }

    @ApiOperation(value = "修改功能")
    @PostMapping("updateFunction")
    public QueryResponse updateFunction(@Valid @RequestBody FunctionUpdateReqDto parmDto,BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        boolean lock = getTokenLock("/api/functions/updateFunction");
        try {
            if(lock) {
                boolean isSucc = functionsService.updateFunction(parmDto);
                if (isSucc) {
                    return new QueryResponse();
                }
            }
        } catch (Exception e) {
            log.error("updateFunction",e);
        } finally {
            relesesTokenLock("/api/functions/updateFunction");
        }
        return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED);
    }

    @ApiOperation(value = "删除功能")
    @PostMapping("deleteFunction")
    public QueryResponse deleteFunction(@RequestParam("functionId")Integer functionId){
        try {
            boolean isSucc = functionsService.deleteFunction(functionId);
            if(isSucc){
                return new QueryResponse();
            }
        } catch (Exception e) {
            log.error("deleteFunction",e);
        }
        return new QueryResponse(ResultStatus.ERROR_DELETE_FAILED);
    }

}
